package com.success;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author MagicMushroom
 * @date 2021/6/26
 */
@ComponentScan(basePackages = "com.success",
        useDefaultFilters = false,
         includeFilters = {
            @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class})
         })
public class SpringMvcConfig extends WebMvcConfigurationSupport {
    // 添加静态资源处理器
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/");
    }

    // 给访问jsp的请求加上前缀（“/”） 和后缀 (".jsp")
    @Override
    protected void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/",".sjp");
    }

//    加了fastjson的依赖后，这里配置引用fastjson，以及设置编码
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        converter.setDefaultCharset(StandardCharsets.UTF_8);

        FastJsonConfig config = new FastJsonConfig();
        config.setCharset(StandardCharsets.UTF_8);

        converter.setFastJsonConfig(config);

        converters.add(converter);
    }
}
